package com.animatedpicture.object;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.ImageIcon;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class Keyboard implements IMovingObject {
	
	private Point Pos;
	private final int one_X = -15,two_X = -10, three_X = 20;
	private final int one_Y = 10,two_Y = 20,three_Y = 10;
	private final int line_gap = 15;
	private int ch = 0;
	private boolean ani_Flag = false;
	
	private AudioStream as;
	
	private String headType;
	private ImageIcon headImg;
	
	public Keyboard(String headType){
		Pos = null;

		this.headType = headType;
		
		// Get audio file from local storage
		try {
			File file = new File("./sound/Keyboard_Sound.wav");
			FileInputStream fis = new FileInputStream(file);
			as = new AudioStream(fis);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.headType = headType;
		
		switch (this.headType) {
		case "Default":
			headImg = null;
			break;
		case "Yap Che Een":
			headImg = new ImageIcon("./Image/Yap.png");
			break;
		case "Kim Ik Soo":
			headImg = new ImageIcon("./Image/Kim.png");
			break;
		case "Wong Kok Sheng":
			headImg = new ImageIcon("./Image/Wong.png");
			break;
		case "Shin Yong Tae":
			headImg = new ImageIcon("./Image/Shin.png");
			break;
		case "Kim Seock Yoon":
			headImg = new ImageIcon("./Image/Kim_S.png");
			break;
		default:
			break;
		}
	}

	@Override
	public void draw(Graphics2D g2) {
		// TODO Auto-generated method stub
		if (Pos != null) {
			Ellipse2D.Double head;
			Line2D.Double body;
			Line2D.Double arm_L;
			Line2D.Double arm_R;
			Line2D.Double leg_L;
			Line2D.Double leg_R;
			
			Graphics g = (Graphics)g2;
			
			if( ani_Flag == true ){
				if (ch == 0) {
					++ch;
					head = new Ellipse2D.Double(Pos.x + 60, Pos.y, 30, 30);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x + 60 - 5, Pos.y - 10, 40, 40, null);
					}
					
					arm_L = new Line2D.Double(Pos.x + 75, Pos.y + 35,
							Pos.x + 75 - 35, Pos.y + 75);
					arm_R = new Line2D.Double(Pos.x + 75, Pos.y + 35,
							Pos.x + 75 + 5, Pos.y + 75);
					body = new Line2D.Double(Pos.x + 75, Pos.y + 30,
							Pos.x + 75, Pos.y + 120);
					leg_L = new Line2D.Double(Pos.x + 75, Pos.y + 120,
							Pos.x + 75 - 20, Pos.y + 150);
					leg_R = new Line2D.Double(Pos.x + 75, Pos.y + 120,
							Pos.x + 75 + 20, Pos.y + 150);
				} else if (ch == 1) {
					++ch;
					head = new Ellipse2D.Double(Pos.x + 60, Pos.y - 5, 30, 30);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x + 60 - 5, Pos.y - 10 - 5, 40, 40, null);
					}
					
					arm_L = new Line2D.Double(Pos.x + 75, Pos.y + 35,
							Pos.x + 75 - 15, Pos.y + 75);
					arm_R = new Line2D.Double(Pos.x + 75, Pos.y + 35,
							Pos.x + 75 + 25, Pos.y + 75);
					body = new Line2D.Double(Pos.x + 75, Pos.y + 30 - 5,
							Pos.x + 75, Pos.y + 110);
					leg_L = new Line2D.Double(Pos.x + 75, Pos.y + 110,
							Pos.x + 75 - 20, Pos.y + 145);
					leg_R = new Line2D.Double(Pos.x + 75, Pos.y + 110,
							Pos.x + 75 + 20, Pos.y + 145);
				} else if (ch == 2) {
					++ch;
					head = new Ellipse2D.Double(Pos.x + 60, Pos.y, 30, 30);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x + 60 - 5, Pos.y - 10, 40, 40, null);
					}
					
					arm_L = new Line2D.Double(Pos.x + 75, Pos.y + 35,
							Pos.x + 75 - 25, Pos.y + 75);
					arm_R = new Line2D.Double(Pos.x + 75, Pos.y + 35,
							Pos.x + 75 + 15, Pos.y + 75);
					body = new Line2D.Double(Pos.x + 75, Pos.y + 30,
							Pos.x + 75, Pos.y + 120);
					leg_L = new Line2D.Double(Pos.x + 75, Pos.y + 120,
							Pos.x + 75 - 20, Pos.y + 150);
					leg_R = new Line2D.Double(Pos.x + 75, Pos.y + 120,
							Pos.x + 75 + 20, Pos.y + 150);
				} else {
					ch = 0;
					head = new Ellipse2D.Double(Pos.x + 60, Pos.y - 5, 30, 30);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x + 60 - 5, Pos.y - 10 - 5, 40, 40, null);
					}
					
					arm_L = new Line2D.Double(Pos.x + 75, Pos.y + 35,
							Pos.x + 75 - 5, Pos.y + 75);
					arm_R = new Line2D.Double(Pos.x + 75, Pos.y + 35,
							Pos.x + 75 + 35, Pos.y + 75);
					body = new Line2D.Double(Pos.x + 75, Pos.y + 30 - 5,
							Pos.x + 75, Pos.y + 110);
					leg_L = new Line2D.Double(Pos.x + 75, Pos.y + 110,
							Pos.x + 75 - 20, Pos.y + 145);
					leg_R = new Line2D.Double(Pos.x + 75, Pos.y + 110,
							Pos.x + 75 + 20, Pos.y + 145);
				}
			}
			else {
				head = new Ellipse2D.Double(Pos.x + 60, Pos.y, 30, 30);
				
				if (headImg != null) {
					g.drawImage(headImg.getImage(),Pos.x + 60 - 5, Pos.y - 10, 40, 40, null);
				}
				
				arm_L = new Line2D.Double(Pos.x + 75, Pos.y + 35,
						Pos.x + 75 - 20, Pos.y + 75);
				arm_R = new Line2D.Double(Pos.x + 75, Pos.y + 35,
						Pos.x + 75 + 20, Pos.y + 75);
				body = new Line2D.Double(Pos.x + 75, Pos.y + 30,
						Pos.x + 75, Pos.y + 120);
				leg_L = new Line2D.Double(Pos.x + 75, Pos.y + 120,
						Pos.x + 75 - 20, Pos.y + 150);
				leg_R = new Line2D.Double(Pos.x + 75, Pos.y + 120,
						Pos.x + 75 + 20, Pos.y + 150);
			}
			
			RoundRectangle2D.Double board = new RoundRectangle2D.Double(Pos.x, Pos.y + 70, 150, 30, 10, 10);
			
			Rectangle2D.Double piano = new Rectangle2D.Double(Pos.x + 15, Pos.y + 70, 120, 10 );

			Line2D.Double l1 = new Line2D.Double(Pos.x + line_gap * 1, Pos.y + 70, Pos.x + line_gap * 1, Pos.y + 80 );
			Line2D.Double l2 = new Line2D.Double(Pos.x + line_gap * 2, Pos.y + 70, Pos.x + line_gap * 2, Pos.y + 80 );
			Line2D.Double l3 = new Line2D.Double(Pos.x + line_gap * 3, Pos.y + 70, Pos.x + line_gap * 3, Pos.y + 80 );
			Line2D.Double l4 = new Line2D.Double(Pos.x + line_gap * 4, Pos.y + 70, Pos.x + line_gap * 4, Pos.y + 80 );
			Line2D.Double l5 = new Line2D.Double(Pos.x + line_gap * 5, Pos.y + 70, Pos.x + line_gap * 5, Pos.y + 80 );
			Line2D.Double l6 = new Line2D.Double(Pos.x + line_gap * 6, Pos.y + 70, Pos.x + line_gap * 6, Pos.y + 80 );
			Line2D.Double l7 = new Line2D.Double(Pos.x + line_gap * 7, Pos.y + 70, Pos.x + line_gap * 7, Pos.y + 80 );
			Line2D.Double l8 = new Line2D.Double(Pos.x + line_gap * 8, Pos.y + 70, Pos.x + line_gap * 8, Pos.y + 80 );
			Line2D.Double l9 = new Line2D.Double(Pos.x + line_gap * 9, Pos.y + 70, Pos.x + line_gap * 9, Pos.y + 80 );
			
			
			Line2D.Double board_leg_L = new Line2D.Double(Pos.x + 25, Pos.y + 100, Pos.x + 25, Pos.y + 150 );
			Line2D.Double L_foot1 = new Line2D.Double( Pos.x + 25, Pos.y + 150 , Pos.x + 25 + one_X, Pos.y + 150 + one_Y);
			Line2D.Double L_foot2 = new Line2D.Double( Pos.x + 25, Pos.y + 150 , Pos.x + 25 + two_X, Pos.y + 150 + two_Y);
			Line2D.Double L_foot3 = new Line2D.Double( Pos.x + 25, Pos.y + 150 , Pos.x + 25 + three_X, Pos.y + 150 + three_Y);

			Line2D.Double board_leg_R = new Line2D.Double(Pos.x + 125, Pos.y + 100, Pos.x + 125, Pos.y + 150 );
			Line2D.Double R_foot1 = new Line2D.Double( Pos.x + 125, Pos.y + 150 , Pos.x + 125 + one_X, Pos.y + 150 + one_Y);
			Line2D.Double R_foot2 = new Line2D.Double( Pos.x + 125, Pos.y + 150 , Pos.x + 125 + two_X, Pos.y + 150 + two_Y);
			Line2D.Double R_foot3 = new Line2D.Double( Pos.x + 125, Pos.y + 150, Pos.x + 125 + three_X, Pos.y + 150 + three_Y);


			g2.setStroke( new BasicStroke( 3 ) );
			g2.setColor( Color.black );
			g2.draw(board_leg_L);
			g2.draw(L_foot1);
			g2.draw(L_foot2);
			g2.draw(L_foot3);
			
			g2.draw(board_leg_R);
			g2.draw(R_foot1);
			g2.draw(R_foot2);
			g2.draw(R_foot3);
			

			g2.setColor( Color.blue );
			
			g2.draw(leg_L);
			g2.draw(leg_R);
			g2.draw(body);
			
			if (headImg == null) {
				g2.fill(head);
				g2.draw(head);
			}

			g2.setColor( Color.black );
			g2.fill(board);
			
			g2.setColor( Color.white );
			g2.fill( piano );
			
			g2.setColor( Color.black );
			g2.draw(board);
			g2.draw(piano);

			g2.setColor( Color.black );
			g2.draw(l1);
			g2.draw(l2);
			g2.draw(l3);
			g2.draw(l4);
			g2.draw(l5);
			g2.draw(l6);
			g2.draw(l7);
			g2.draw(l8);
			g2.draw(l9);
			

			g2.setColor( Color.blue );
			g2.draw(arm_L);
			g2.draw(arm_R);
			
		}
	}

	public void setPosition(Point mPoint) {
		// TODO Auto-generated method stub

		Pos = mPoint;
		System.out.println("ObjectCar SetPosition : ("+mPoint.x+","+mPoint.y+")");
	}
	
	public void animate(boolean trriger){
		ani_Flag = trriger;
	}

	@Override
	public void playMusic(boolean trriger) {
		if (trriger) {
			AudioPlayer.player.start(as);
		}else {
			AudioPlayer.player.stop(as);
		}
	}

}
